package mechanism;

// The double-check idiom for lazy initialization - (broken?)
public class SingletonInitialization {

	private static volatile Foo foo = null;
	
	public static Foo getFoo() {
		if(foo == null)
		{
			synchronized(SingletonInitialization.class)
			{
				if(foo == null) {
					foo = new Foo();
				}
			}
		}
		return foo;
	}
}

// Normal static initialization (not lazy)
class InitializationApp2 {

	private static Foo foo = new Foo();
	
	public static Foo getFoo() {
		return foo;
	}
}

// Properly synchronized lazy initialization
class InitializationApp3 {

	private static Foo foo = null;
	
	public synchronized static Foo getFoo() {
		if (foo == null)
		{
			foo = new Foo();
		}
		return foo;
	}
}

// The initialize-on-demand holder class idiom
class InitializationApp4 {

	private static class FooHolder{
		static final Foo foo = new Foo();
	}
	
	public static Foo getFoo() {
		return FooHolder.foo;
	}
}

class Foo{}